package com.wcj.array;

/**
 * 给定一个字符串 s ，你需要反转字符串中每个单词的字符顺序，同时仍保留空格和单词的初始顺序。
 * 输入：s = "Let's take LeetCode contest"
 * 输出："s'teL ekat edoCteeL tsetnoc"
 */
public class 反转字符串中的单词III {


    public String reverseWords(String s) {

        String[] s1 = s.trim().split(" ");
        StringBuffer result = new StringBuffer();
        for (int i = 0; i < s1.length - 1; i++){
            String trim = s1[i].trim();
            if (!s1[i].trim().equals(" ")){
                for (int j = trim.length() - 1; j >= 0; j--){
                    result.append(trim.charAt(j));
                }
                result.append(" ");
            }
        }
        String trim2 = s1[s1.length - 1].trim();
        if (!s1[s1.length - 1].trim().equals(" ")){
            for (int j = trim2.length() - 1; j >= 0; j--){
                result.append(trim2.charAt(j));
            }
        }
        return String.valueOf(result);
    }


}
